<?php

namespace app\web\controller;
use app\web\model\ConsMemberModel;
use app\common\controller\StoreBaseController;
use think\Db;
/**
 * 消费会员列表
 */
class ConsMemberController extends StoreBaseController
{
 	 protected $model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new ConsMemberModel();
    }
    /**
     * 消费会员列表
     * @author [longx] 
     */
    public function index( $page = 1){
		$field = 'm.*,g.id as uid,g.mobile,g.real_name,g.city';
		$where['m.agent_id'] = session('storeInfo')['id'];

		if(request()->isPost()){
		 	$param = input('post.');
			//用户名称
			if($param['realname']){
				$where['g.real_name'] = ['like', '%'.$param['realname'].'%'];
			}
			//手机号码
			if($param['mobile']){
				$where['g.mobile'] = ['like', '%'.$param['mobile'].'%'];
			}	
			//创建时间
			if($param['start_time']&&$param['end_time']){
				$where['m.time'] = ['between time', [$param['start_time'],$param['end_time']]];
			}	 			 				
	 	}
		$list=Db::name("mall_consmember")->alias('m')->join('__MEMBER__ g','g.id=m.u_id')->field($field)->where($where)->order("m.time desc")->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }

    /**
     * [del删除]
     * @author [longx] 
     */
    public function dc(){
$field = 'm.*,g.id,g.mobile,g.real_name,g.city';
		$where['m.agent_id'] = session('storeInfo')['id'];
$result=Db::name("mall_consmember")->alias('m')->join('__MEMBER__ g','g.id=m.u_id')->field($field)->where($where)->select();
        $str = "姓名,电话,地址,积分,消费时间,\n"; 
        $str = iconv('utf-8','gbk',$str);

        foreach($result as $k=>$row){
                $name = iconv('utf-8','gbk',$row['real_name']); //中文转码 
                $mobile = iconv('utf-8','gbk',$row['mobile']);                 
                $address = iconv('utf-8','gbk',$row['city']);                 
                $score = iconv('utf-8','gbk',$row['score']);                 
                $time = iconv('utf-8','gbk',$row['time']);                 
                $str .= $name.",".$mobile.",".$address.",".$score.",".$time.","."\n"; //用引文逗号分开 
            } 
      

        	$filename ='会员消费表.csv';   //设置文件名  
  
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;

     }

    
    
    
    
    
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}